﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NPMonitor.Implementation;
using NPMonitor.DataModel;

namespace NPMonitor.Web.UserControls
{
    public partial class JobHistory : BaseUserControl
    {
        protected int ItemIndex
        {
            get;
            set;
        }
        protected int PageSize
        {
            get
            {
                return ctPager.PageSize;
            }
        }
        protected string MachineName
        {
            get
            {
                return Request.QueryString[CommonString.MachineName.ToLower()] as string;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //ddlStatus.Visible = false;
            ctPager.PageChange += new PagerControl.PageRefresh(BindRepeater);

            if (!IsPostBack)
            {
                txtStartDate.Text = DateTime.UtcNow.AddMonths(-1).ToString(CommonString.DataTimeFormat);
                txtEndDate.Text = DateTime.UtcNow.ToString(CommonString.DataTimeFormat);
                BindStatus();
                BindRepeater(0);
            }
        }

        protected void BindRepeater(int pageIndex)
        {
            int total;
            ItemIndex = pageIndex * PageSize + 1;
            Nullable<JobStatus> status = null;

            if (ddlStatus.SelectedIndex > 0)
            {
                status = (JobStatus)Enum.Parse(typeof(JobStatus), ddlStatus.SelectedValue, false);
            }

            DateTime startTime = DateTime.UtcNow.Date.AddMonths(-1);
            DateTime endTime = DateTime.UtcNow.Date.AddDays(1);
            if (txtStartDate.Text != string.Empty)
            {
                DateTime.TryParseExact(txtStartDate.Text, CommonString.DataTimeFormat, null, System.Globalization.DateTimeStyles.None, out startTime);
            }

            if (txtEndDate.Text != string.Empty)
            {
                DateTime.TryParseExact(txtEndDate.Text, CommonString.DataTimeFormat, null, System.Globalization.DateTimeStyles.None, out endTime);
            }

            rptJobHistoryList.DataSource =
                StoreManager.Current.JobStore.GetJobExecutionHistories(MachineName, pageIndex, PageSize, txtJobName.Text, status, startTime, endTime, out total);
            rptJobHistoryList.DataBind();

            litItemCount.Text = total.ToNullFilteredString();
            ctPager.IntialProperties(total);
            txtErrorMessage.Text = string.Empty;
        }

        protected void imgRefresh_OnClick(object sender, EventArgs e)
        {
            BindRepeater(ctPager.CurrentPageIndex);
        }

        protected void BindStatus()
        {
            List<JobStatus> statusList = new List<JobStatus>();

            statusList.Add(JobStatus.Done);
            statusList.Add(JobStatus.Fail);
            statusList.Add(JobStatus.ExecutionTimeout);

            ddlStatus.BindDataSource(statusList, null, null);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            ctPager.CurrentPageIndex = 0;
            //ddlStatus. = false;// Style.Add("disable", "false");
            BindRepeater(0);

        }

        public string Status 
        {
            get 
            {
                return (string)ViewState[""];
            }
            set { ViewState[""] = value; }
        }

        
    }
}